package com.kin207.netty.msg.parse;

public class MessageConversion {
	private final static String startWith ="68";
	private final static String endWith ="0016";
	private final static String inputService ="61";
	private final static Integer index = 1;
	private final static Integer sendType = 1;
	private final static Integer deviceType = 1;
	private final static Integer deviceId = 4;
	private final static Integer random= 2;
	private final static Integer serviceType = 2;
	private final static Integer timestamp = 4;
	private final static Integer count = 2;
	private final static Integer flag = 1;
	private final static Integer dataLength = 1;
	private final static Integer end =2;
	private final static Integer multiple = 2;
	
	public static String getDeviceId(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getBaseLength(), getDeviceIdEnd());
		}
		return null;
	}


	public static String getRandom(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getDeviceIdEnd(), getRandomEnd());
		}
		return null;
	}
	public static String getServiceType(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getRandomEnd(),getServiceTypeEnd());
		}
		return null;
	}
	public static String getTimestamp(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getServiceTypeEnd(), getTimestampEnd());
		}
		return null;
	}
	public static Integer getCount(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
			String length =msg.substring(getTimestampEnd(), getCountEnd());
			return Integer.parseInt(length);
		}
		return null;
	}
	
	public static String getFlag(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
		  return msg.substring(getCountEnd(), getFlagEnd());
		}
		return null;
	}
	public static String getData(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
			return msg.substring(getDataLengthEnd(),getDataLengthEnd()+getDataLength(msg));
		}
		return null;
	}
	public static String getDeviceCode(String msg) {
		if(!msg.startsWith(startWith)) {
			return null;
		}
		if(getFlag(msg).equals(inputService)) {
			return getData(msg);
		}
		return null;
	}
	public static Integer getBaseLength() {
		return (index+sendType+deviceType)*multiple;
	}
	public static Integer getDeviceIdEnd() {
		return getBaseLength()+deviceId*multiple;
	}
	public static Integer getRandomEnd() {
		return getDeviceIdEnd()+random*multiple;
	}
	public static Integer getServiceTypeEnd() {
		return  getRandomEnd() + serviceType*multiple;
	}
	public static Integer getTimestampEnd() {
		return getServiceTypeEnd()+timestamp*multiple;
	}
	public static Integer getCountEnd() {
		return	getTimestampEnd()+count*multiple;
	}
	public static Integer getFlagEnd() {
		return  getCountEnd() + flag*multiple;
	}	
	public static Integer getDataLengthEnd() {
		return getFlagEnd()+dataLength*multiple;
	}
	public static Integer getDataLength(String msg) {
		if (msg.startsWith(startWith)&&msg.endsWith(endWith)) {
			String length = msg.substring(getFlagEnd(), getDataLengthEnd());
			return Integer.parseInt(length)*multiple;
		}
		return null;
	}
	public static void main(String[] args) {
		System.out.println(getDeviceId("68030109801541122000015F1B292600036001XX0016"));
		System.out.println(getRandom("68030109801541122000015F1B292600036001XX0016"));
		System.out.println(getServiceType("68030109801541122000015F1B292600036001XX0016"));
		System.out.println(getTimestamp("68030109801541122000015F1B292600036001XX0016"));
		System.out.println(getCount("68030109801541122000015F1B292600036001XX0016"));
		System.out.println(getFlag("68030109801541122000015F1B292600036001XX0016"));
		System.out.println(getDataLength("68030109801541122000015F1B292600036001XX0016"));
		System.out.println(getData("68030109801541122000015F1B292600036001XX0016"));
		System.out.println(getDeviceCode("68030109801541122000015F1B292600076105XXXXXXXXXX0016"));
		
	}
}
